package com.xinweisi.dataserver.module.memberdata.onlinemember;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinweisi.dataserver.common.domain.PageResultDTO;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.dto.OnLineMemberQueryDTO;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.entity.AreaEntity;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.entity.OnLineMemberEntity;
import com.xinweisi.dataserver.module.memberdata.onlinemember.domain.vo.OnLineMemberVO;
import com.xinweisi.dataserver.util.SmartPageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class OnlineMemberService {

    @Autowired
    private OnlineMemberDao onlineMemberDao;
    /**
     * 分页获取在线会员
     *
     * @param queryDTO
     * @return
     */
    public ResponseDTO<PageResultDTO<OnLineMemberVO>> getOnlineMember(OnLineMemberQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        /*if(queryDTO.getAccount() != null){
            queryDTO.setAccount(queryDTO.getAccount().toLowerCase());
        }*/
        List<OnLineMemberEntity> entityList = onlineMemberDao.selectOnLineMemberList(queryDTO);
        for (OnLineMemberEntity entity:entityList) {
            String areaName = " ";
            List<AreaEntity> areaEntityList = onlineMemberDao.selectAreaListByIds(entity.getAreaId());
            for (AreaEntity q:areaEntityList) {
                areaName+=q.getName()+",";
            }
            entity.setAreaName(areaName.substring(0,areaName.length()-1));
        }
        page.setTotal(entityList.size());
        PageResultDTO<OnLineMemberVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, SmartPageUtil.startPage(entityList,queryDTO.getPageNum(),queryDTO.getPageSize()), OnLineMemberVO.class);
        return ResponseDTO.succData(pageResultDTO);
    }

}
